/**
*
*/
package org.javacommerce.paypal.download.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.javacommerce.core.log.LogMessage;
import org.javacommerce.core.log.LogMessageFactory;
import org.javacommerce.paypal.PayPalDownloadException;
import org.javacommerce.paypal.download.AbstractDownload;
/**
* Abstract class to handle download of reports via HTTP from PayPal. Username and Password should be those created in
* multi-user access and given the permission "Settlement File & Scheduled Downloads".
* @author Michael Blanton (mike@mikeblanton.com)
*/
public abstract class AbstractHttpDownload extends AbstractDownload {
private static final Log LOG = LogFactory.getLog(AbstractHttpDownload.class);
private static final LogMessage LOGM = LogMessageFactory.getInstance();
public AbstractHttpDownload() {
super();
}
/**
* @param _username
* @param _password
*/
public AbstractHttpDownload(String _username, String _password) {
super(_username, _password);
}
public final Reader downloadFile(String _month, String _day, String _year) throws PayPalDownloadException, IOException {
long startTime = System.currentTimeMillis();
if (getDownloadUser() == null || getDownloadUser().length() == 0) {
long endTime = System.currentTimeMillis();
LOGM.logFailure("AbstractHttpDownload.downloadFile", startTime, endTime, "username not configured");
throw new PayPalDownloadException("Error downloading file - username not configured.");
}
if (getDownloadPassword() == null || getDownloadPassword().length() == 0) {
long endTime = System.currentTimeMillis();
LOGM.logFailure("AbstractHttpDownload.downloadFile", startTime, endTime, "password not configured");
throw new PayPalDownloadException("Error downloading file - password not configured.");
}
StringWriter writer = new StringWriter();
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(getURL());
post.addParameter("day", _day);
post.addParameter("month", _month);
post.addParameter("year", _year);
post.addParameter("uname", getDownloadUser());
post.addParameter("pword", getDownloadPassword());
if (LOG.isDebugEnabled()) {
LOG.debug("Downloading log from [" + getURL() + "]");
}
try {
client.executeMethod(post);
BufferedReader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
PrintWriter pw = new PrintWriter(writer);
String line = null;
while ((line = reader.readLine()) != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Read line [" + line + "]");
}
pw.println(line);
}
pw.close();
reader.close();
} catch (HttpException e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error downloading log: " + e.getLocalizedMessage(), e);
}
long endTime = System.currentTimeMillis();
LOGM.logFailure("AbstractHttpDownload.downloadFile", startTime, endTime, "Error downloading log");
throw e;
} catch (IOException e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error downloading log: " + e.getLocalizedMessage(), e);
}
long endTime = System.currentTimeMillis();
LOGM.logFailure("AbstractHttpDownload.downloadFile", startTime, endTime, "Error downloading log");
throw e;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Download complete.");
}
String response = writer.toString();
writer.close();
long endTime = System.currentTimeMillis();
LOGM.logSuccess("AbstractHttpDownload.downloadFile", startTime, endTime);
return new StringReader(response);
}
/* (non-Javadoc)
* @see org.javacommerce.paypal.download.AbstractDownload#getURL()
*/
public abstract String getURL();
}